import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/models/team_user_entity.dart';
import 'package:seedfour/app/theme.dart';
import 'package:seedfour/app/widget/tag.dart';

import '../../../constants.dart';
import 'logic.dart';

class MineTeamPage extends StatelessWidget {
  MineTeamPage({Key? key}) : super(key: key);

  final logic = Get.find<MineTeamLogic>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('分身'),
        leading: IconButton(
          icon: Icon(
            Icons.arrow_back_ios,
            size: 16.w,
          ),
          onPressed: () {
            Get.back();
          },
        ),
        actions: [
          IconButton(
            icon: const Icon(Icons.add),
            onPressed: () => showDialog(
              context: context,
              barrierDismissible: false,
              builder: _buildAddDialog,
            ),
          ),
        ],
      ),
      body: _buildListItemBg(builder: _itemBuilder),
    );
  }

  Widget _buildAddDialog(BuildContext context) {
    return AlertDialog(
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(kRadiusMedium),
      ),
      title: Text(
        '添加分身',
        style: TextStyle(
          fontSize: kFontSizeMedium,
          color: AppColor.colorFontBlack,
          fontWeight: AppFont.fontMedium,
        ),
      ),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          TextField(
            decoration: const InputDecoration(
              hintText: '* 成员姓名',
              hintStyle: TextStyle(color: AppColor.colorFontHint),
              border: OutlineInputBorder(
                borderSide: BorderSide(color: AppColor.colorLineGrey),
                borderRadius: BorderRadius.all(Radius.circular(10.0)),
              ),
              contentPadding: EdgeInsets.all(8.0),
            ),
            controller: logic.nameController,
          ),
          SizedBox(height: kSpaceSmall),
          TextField(
            decoration: const InputDecoration(
              hintText: '* 成员手机号',
              hintStyle: TextStyle(color: AppColor.colorFontHint),
              border: OutlineInputBorder(
                borderSide: BorderSide(color: AppColor.colorLineGrey),
                borderRadius: BorderRadius.all(Radius.circular(10.0)),
              ),
              contentPadding: EdgeInsets.all(8.0),
            ),
            controller: logic.mobileController,
          ),
        ],
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.pop(context),
          child: const Text('取消'),
        ),
        TextButton(
          onPressed: () => {Navigator.pop(context), logic.onAddItem()},
          child: const Text('确定'),
        ),
      ],
    );
  }

  List<Widget> _itemBuilder(TeamUserData item) {
    return [
      Row(
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  item.name ?? '',
                  style: TextStyle(
                    fontSize: kFontSizeMedium,
                    fontWeight: AppFont.fontMedium,
                    color: AppColor.colorFontBlack,
                  ),
                ),
                SizedBox(height: kSpaceMini),
                Text(
                  item.mobile ?? '',
                  style: TextStyle(
                    fontSize: kFontSizeSmall,
                    color: AppColor.colorFontGrey,
                  ),
                ),
              ],
            ),
          ),
          Tag(
            text: item.role == 1 ? '老板' : '成员',
            textColor: item.role == 1
                ? AppColor.colorFontBlue
                : AppColor.colorFontGrey,
            textSize: kFontSizeMini,
            borderColor: item.role == 1
                ? AppColor.colorFontBlue
                : AppColor.colorFontGrey,
            backgroundColor:
                item.role == 1 ? AppColor.colorTagBlue : AppColor.colorTagGrey,
            padding: EdgeInsets.symmetric(
              horizontal: 6.w,
              vertical: 2.w,
            ),
          ),
        ],
      ),
    ];
  }

  Widget _renderRow(BuildContext context, int index) {
    if (index < logic.teamUsers.length) {
      return InkWell(
        onLongPress: () => showDialog<String>(
          context: context,
          barrierDismissible: false,
          builder: (BuildContext context) => AlertDialog(
            title: const Text('提示'),
            content: Text('是否删除成员${logic.teamUsers[index].name}？'),
            actionsPadding: EdgeInsets.all(kSpaceMini),
            actions: [
              TextButton(
                onPressed: () => Navigator.pop(context, '再想想'),
                child: const Text('再想想'),
              ),
              TextButton(
                  onPressed: () =>
                      {logic.onDeleteItem(index), Navigator.pop(context, '删除')},
                  child:
                      const Text('删除', style: TextStyle(color: Colors.grey))),
            ],
          ),
        ),
        child: Container(
          margin: EdgeInsets.only(
              left: kSpaceSmall,
              right: kSpaceSmall,
              top: kSpaceMedium,
              bottom: index == logic.teamUsers.length - 1 ? kSpaceMedium : 0),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(kRadiusMedium),
          ),
          padding: EdgeInsets.all(kSpaceSmall),
          child: Column(
            children: _itemBuilder(logic.teamUsers[index]),
          ),
        ),
      );
    } else {
      return _getMoreWidget();
    }
  }

  Widget _getMoreWidget() {
    return Center(
      child: Padding(
        padding: EdgeInsets.all(kSpaceMedium),
        child: Text(
          logic.bottomTips.value,
          style: TextStyle(fontSize: kFontSizeSmall, color: Colors.grey),
        ),
      ),
    );
  }

  Widget _buildListItemBg({
    required List<Widget> Function(TeamUserData item) builder,
  }) {
    return Obx(() {
      if (logic.isLoading.value) {
        return const Center(child: CircularProgressIndicator());
      } else {
        return ListView.builder(
          itemBuilder: _renderRow,
          itemCount: logic.teamUsers.length + 1,
          controller: logic.scrollController,
        );
      }
    });
  }
}
